package com.chensf.studydemo.common;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.ContextThemeWrapper;
import android.view.animation.DecelerateInterpolator;
import android.widget.Checkable;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;

import com.chensf.studydemo.R;


/**
 * @author Heycz
 * 2018/6/17
 */
public class McCheckBox extends AppCompatImageView implements Checkable {

    private boolean mChecked;
    private OnCheckChangeListener mOnCheckChangeListener;
    private boolean enableToggleOnClick = true;
    private boolean mCountable;
    private Drawable singleCheckDrawable;
    private TextDrawable textDrawable;

    private static final int[] CHECKED_STATE_SET = {
            android.R.attr.state_checked
    };

    public McCheckBox(Context context) {
        this(context, null);
    }

    public McCheckBox(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public McCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(new ContextThemeWrapper(context, R.style.CTCheckBox), attrs, defStyleAttr);
        setClickable(true);
        singleCheckDrawable = getDrawable();
    }

    @Override
    public boolean performClick() {
        if (enableToggleOnClick) {
            toggle();
        }
        return super.performClick();
    }

    @Override
    public void setChecked(boolean checked) {
        setChecked(checked, true);
    }

    public void setChecked(boolean check, boolean anim) {
        if (this.mChecked != check) {
            this.mChecked = check;

            refreshDrawableState();
            if (mOnCheckChangeListener != null) {
                mOnCheckChangeListener.onCheckChange(this, check);
            }
            if (check && anim) {
                AnimatorSet animatorSet = new AnimatorSet();
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(this, "scaleX", 0.8f, 1f, 1.2f, 1f);
                ObjectAnimator scaleY = ObjectAnimator.ofFloat(this, "scaleY", 0.8f, 1f, 1.2f, 1f);
                animatorSet.setDuration(300);
                animatorSet.setInterpolator(new DecelerateInterpolator());
                animatorSet.play(scaleX).with(scaleY);//两个动画同时开始
                animatorSet.start();
            }
        }
    }

    public void setCheckedNum(int checkedNum) {
        if (!mCountable) {
            setChecked(checkedNum > 0);
            return;
        }
        if (checkedNum < 1) {
            setChecked(false);
            return;
        }
        textDrawable.setText(String.valueOf(checkedNum));
        if (isChecked()) {
            invalidate();
        } else {
            setChecked(true);
        }
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }

    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }
        return drawableState;
    }

    /**
     * 设置performClick时是否调用toggle
     */
    public void enableToggleOnClick(boolean enable) {
        this.enableToggleOnClick = enable;
    }

    public void setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener) {
        this.mOnCheckChangeListener = onCheckChangeListener;
    }

    public void setCountable(boolean countable) {
        if (mCountable != countable) {
            mCountable = countable;
            if (mCountable) {
                setImageDrawable(createBackgroundDrawable());
            } else {
                setImageDrawable(singleCheckDrawable);
            }
        }
    }

    private Drawable createBackgroundDrawable() {
        StateListDrawable drawable = new StateListDrawable();
        drawable.setBounds(new Rect(0, 0,
                singleCheckDrawable.getIntrinsicWidth(), singleCheckDrawable.getIntrinsicHeight()));
        //选中
        textDrawable = TextDrawable.builder().beginConfig()
                .withBorder(4, Color.WHITE).textColor(Color.WHITE)
                .width(singleCheckDrawable.getIntrinsicWidth())
                .height(singleCheckDrawable.getIntrinsicHeight())
                .endConfig()
                .round().build("", ContextCompat.getColor(getContext(), R.color.A06));
        drawable.addState(new int[]{android.R.attr.state_selected}, textDrawable);
        drawable.addState(new int[]{android.R.attr.state_checked}, textDrawable);
        //禁用
        Drawable disable = ContextCompat.getDrawable(getContext(), R.drawable.ct_icon_checkbox_disable);
        drawable.addState(new int[]{-android.R.attr.state_enabled}, disable);
        //默认
        Drawable normal = ContextCompat.getDrawable(getContext(), R.drawable.ct_icon_checkbox_default);
        drawable.addState(new int[]{}, normal);
        return drawable;
    }

    public interface OnCheckChangeListener {
        void onCheckChange(McCheckBox view, boolean check);
    }

}